<template>
  <div class="container-content">
    <el-form
      ref="formRef"
      :model="formObject"
      label-width="100px"
      status-icon
      label-position="left"
      :rules="rules"
    >
      <el-dropdown
        style="position: absolute; right: 50px; top: 20px; z-index: 10"
        trigger="click"
        @command="handleCommand"
      >
        <el-button size="small" type="primary">
          选单<i class="el-icon-arrow-down el-icon--right"></i>
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="recOrder">应收单下推</el-dropdown-item>
          <el-dropdown-item command="otherOrder"
            >其他应付单下推</el-dropdown-item
          >
          <el-dropdown-item command="engineering">工程下推</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <el-collapse v-model="activeName" @change="handleClick">
        <el-collapse-item title="基本信息" name="basic">
          <div class="desc-title">基本</div>
          <el-row :gutter="15">
            <el-col v-if="rowId" :span="8">
              <el-form-item
                label="单据类型"
                prop="kingFinanceReceiptEntity.docTypeCode"
              >
                <el-input
                  :disabled="true"
                  style="width: 100%"
                  v-model="formObject.kingFinanceReceiptEntity.docTypeCode"
                  :props="props"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col v-if="!rowId" :span="8">
              <el-form-item
                label="单据类型"
                prop="kingFinanceReceiptEntity.docTypeCode"
              >
                <el-select
                  v-model="formObject.kingFinanceReceiptEntity.docTypeCode"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in documentTypeList"
                    :key="item.keyCode"
                    :label="item.title"
                    :value="item.keyCode"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="单据编号"
                prop="kingFinanceReceiptEntity.docNo"
              >
                <el-input
                  v-model="formObject.kingFinanceReceiptEntity.docNo"
                  placeholder="保存时自动生成"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item
                label="收款组织"
                prop="kingFinanceReceiptEntity.receiptDeptTitle"
              >
                <el-input
                  v-model="formObject.kingFinanceReceiptEntity.receiptDeptTitle"
                  placeholder="请选择收款组织"
                  clearable
                  :style="{ width: '100%' }"
                >
                  <i
                    slot="suffix"
                    class="el-input__icon el-icon-notebook-2"
                    style="cursor: pointer"
                    @click="addDialog('receiptDeptTitle')"
                  ></i>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="付款单位类型"
                label-width="110px"
                prop="kingFinanceReceiptEntity.payDeptType"
              >
                <el-select
                  @change="payTypeChange"
                  v-model="formObject.kingFinanceReceiptEntity.payDeptType"
                  placeholder="请选择付款单位类型"
                  style="width: 100%"
                >
                  <el-option
                    key="0"
                    label="客户"
                    value="BD_Customer"
                  ></el-option>
                  <el-option
                    key="1"
                    label="供应商"
                    value="BD_Supplier"
                  ></el-option>
                  <el-option
                    key="2"
                    label="部门"
                    value="BD_Department"
                  ></el-option>
                  <el-option
                    key="3"
                    label="员工"
                    value="BD_Empinfo"
                  ></el-option>
                  <el-option
                    key="4"
                    label="其他往来单位"
                    value="FIN_OTHERS"
                  ></el-option>
                  <el-option
                    key="5"
                    label="组织机构"
                    value="ORG_Organization"
                  ></el-option>
                  <el-option key="6" label="银行" value="BD_BANK"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="付款单位"
                prop="kingFinanceReceiptEntity.payDeptName"
              >
                <el-input
                  :disabled="!payType"
                  v-model="formObject.kingFinanceReceiptEntity.payDeptName"
                  placeholder="请选择付款单位"
                  clearable
                  :style="{ width: '100%' }"
                >
                  <i
                    slot="suffix"
                    class="el-input__icon el-icon-notebook-2"
                    style="cursor: pointer"
                    @click="addDialog('payDeptName')"
                  ></i>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="币别"
                prop="kingFinanceReceiptEntity.currencyName"
              >
                <el-input
                  v-model="formObject.kingFinanceReceiptEntity.currencyName"
                  placeholder="请选择币别"
                  clearable
                  readonly
                  :style="{ width: '100%' }"
                >
                  <i
                    slot="suffix"
                    class="el-input__icon el-icon-notebook-2"
                    style="cursor: pointer"
                    @click="addDialog('currency')"
                  ></i>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="销售部门"
                prop="kingFinanceReceiptEntity.saleDeptId"
              >
                <el-input
                  v-model="formObject.kingFinanceReceiptEntity.saleDeptId"
                  placeholder="请输入销售部门"
                  clearable
                  disabled
                  :style="{ width: '100%' }"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="应收金额"
                prop="kingFinanceReceiptEntity.copeMoney"
              >
                <el-input
                  v-model="formObject.kingFinanceReceiptEntity.copeMoney"
                  placeholder="请输入应收金额"
                  clearable
                  disabled
                  :style="{ width: '100%' }"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="实收金额"
                prop="kingFinanceReceiptEntity.actualMoney"
              >
                <el-input
                  v-model="formObject.kingFinanceReceiptEntity.actualMoney"
                  placeholder="请输入实收金额"
                  clearable
                  disabled
                  :style="{ width: '100%' }"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="业务日期"
                prop="kingFinanceReceiptEntity.businessTime"
              >
                <el-form-item
                  prop="inOutTime"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-date-picker
                    v-model="formObject.kingFinanceReceiptEntity.businessTime"
                    type="date"
                    placeholder="选择业务日期"
                    style="width: 100%"
                    value-format="yyyy-MM-dd"
                    format="yyyy-MM-dd"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item
                label="往来单位类型"
                label-width="110px"
                prop="kingFinanceReceiptEntity.transactionCompanyType"
              >
                <el-select
                  @change="gobackTypeChange"
                  v-model="
                    formObject.kingFinanceReceiptEntity.transactionCompanyType
                  "
                  placeholder="请选择往来单位类型"
                  style="width: 100%"
                >
                  <el-option
                    key="1"
                    label="供应商"
                    value="BD_Supplier"
                  ></el-option>
                  <el-option
                    key="2"
                    label="部门"
                    value="BD_Department"
                  ></el-option>
                  <el-option
                    key="3"
                    label="员工"
                    value="BD_Empinfo"
                  ></el-option>
                  <el-option
                    key="4"
                    label="其他往来单位"
                    value="FIN_OTHERS"
                  ></el-option>
                  <el-option
                    key="5"
                    label="组织机构"
                    value="ORG_Organization"
                  ></el-option>
                  <el-option key="6" label="银行" value="BD_BANK"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="往来单位"
                prop="kingFinanceReceiptEntity.transactionCompanyTitle"
              >
                <el-input
                  :disabled="!gobackType"
                  v-model="
                    formObject.kingFinanceReceiptEntity.transactionCompanyTitle
                  "
                  placeholder="请选择往来单位"
                  clearable
                  :style="{ width: '100%' }"
                >
                  <i
                    slot="suffix"
                    class="el-input__icon el-icon-notebook-2"
                    style="cursor: pointer"
                    @click="addDialog('transactionCompany')"
                  ></i>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="销售员"
                prop="kingFinanceReceiptEntity.saleUserName"
              >
                <el-input
                  v-model="formObject.kingFinanceReceiptEntity.saleUserName"
                  placeholder="请输入销售员"
                  clearable
                  :style="{ width: '100%' }"
                >
                  <i
                    slot="suffix"
                    class="el-input__icon el-icon-notebook-2"
                    style="cursor: pointer"
                    @click="addDialog('saleId')"
                  ></i>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <div class="desc-title">整单金额</div>
          <el-row :gutter="10">
            <el-col :span="8">
              <el-form-item
                label="币别"
                prop="kingFinanceReceiptEntity.currencyName"
              >
                <el-input
                  v-model="formObject.kingFinanceReceiptEntity.currencyName"
                  placeholder="请选择币别"
                  clearable
                  readonly
                  :style="{ width: '100%' }"
                >
                  <i
                    slot="suffix"
                    class="el-input__icon el-icon-notebook-2"
                    style="cursor: pointer"
                    @click="addDialog('currency')"
                  ></i>
                </el-input>
              </el-form-item>
            </el-col>
            <!-- <el-col :span="8">
              <el-form-item
                label-width="150px"
                label="付款金额本位币"
                prop="kingFinanceReceiptEntity.receiptAmountCurrency"
              >
                <el-input
                  v-model="formObject.kingFinanceReceiptEntity.receiptAmountCurrency"
                  clearable
                  disabled
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col> -->
            <el-col :span="8">
              <el-form-item
                label-width="150px"
                label="实收金额本位币"
                prop="kingFinanceReceiptEntity.actualMoneyCurrency"
              >
                <el-input
                  v-model="
                    formObject.kingFinanceReceiptEntity.actualMoneyCurrency
                  "
                  clearable
                  disabled
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label-width="150px"
                label="应收金额本位币"
                prop="kingFinanceReceiptEntity.copeMoneyCurrency"
              >
                <el-input
                  v-model="
                    formObject.kingFinanceReceiptEntity.copeMoneyCurrency
                  "
                  clearable
                  disabled
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <!-- <el-col :span="8">
              <el-form-item
                label-width="150px"
                label="付款金额"
                prop="kingFinanceReceiptEntity.payAmount"
              >
                <el-input
                  v-model="formObject.kingFinanceReceiptEntity.payAmount"
                  clearable
                  disabled
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col> -->
            <el-col :span="8">
              <el-form-item
                label="结算币别"
                prop="kingFinanceReceiptEntity.settlementCurrencyName"
              >
                <el-input
                  v-model="
                    formObject.kingFinanceReceiptEntity.settlementCurrencyName
                  "
                  clearable
                  readonly=""
                  :style="{ width: '100%' }"
                >
                  <i
                    slot="suffix"
                    class="el-input__icon el-icon-notebook-2"
                    style="cursor: pointer"
                    @click="addDialog('settlementCurrencyName')"
                  ></i>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label-width="150px"
                label="汇率"
                prop="kingFinanceReceiptEntity.rate"
              >
                <el-input
                  v-model="formObject.kingFinanceReceiptEntity.rate"
                  clearable
                  disabled
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label-width="150px"
                label="结算汇率"
                prop="kingFinanceReceiptEntity.settlementRate"
              >
                <el-input
                  v-model="formObject.kingFinanceReceiptEntity.settlementRate"
                  clearable
                  disabled
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>
        <el-collapse-item title="明细" name="detail">
          <el-button
            type="text"
            @click="addNewRow('kingFinanceReceiptDetailEntities')"
            >新增行</el-button
          >
          <el-table
            :data="formObject.kingFinanceReceiptDetailEntities"
            style="width: 100%"
            highlight-current-row
            border
          >
            <el-table-column
              type="index"
              label="行号"
              width="50"
              align="center"
            >
            </el-table-column>
            <el-table-column label="操作" width="60">
              <template slot-scope="scope">
                <el-button
                  plain
                  icon="el-icon-minus"
                  size="mini"
                  @click="
                    handleDataDelete(
                      scope.$index,
                      scope.row,
                      'kingFinanceReceiptDetailEntities'
                    )
                  "
                ></el-button>
              </template>
            </el-table-column>
            <el-table-column width="150" label="">
              <template slot="header">
                结算方式<span class="required">*</span>
              </template>
              <template slot-scope="scope">
                <el-form-item
                  label-width="0px"
                  style="margin: 0"
                  :rules="rules.kingSettlementTypeTitle"
                  :prop="
                    'kingFinanceReceiptDetailEntities.' +
                    scope.$index +
                    '.kingSettlementTypeTitle'
                  "
                >
                  <el-input v-model="scope.row.kingSettlementTypeTitle">
                    <i
                      slot="suffix"
                      class="el-input__icon el-icon-notebook-2"
                      style="cursor: pointer"
                      @click="addDialog('settlementType', scope.$index)"
                    ></i>
                  </el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column width="150" label="">
              <template slot="header">
                收款用途<span class="required">*</span>
              </template>
              <template slot-scope="scope">
                <el-form-item
                  label-width="0px"
                  style="margin: 0"
                  :rules="rules.payReceiptPurposeTitle"
                  :prop="
                    'kingFinanceReceiptDetailEntities.' +
                    scope.$index +
                    '.payReceiptPurposeTitle'
                  "
                >
                  <el-input v-model="scope.row.payReceiptPurposeTitle">
                    <i
                      slot="suffix"
                      @click="addDialog('payReceiptPurposeCode', scope.$index)"
                      class="el-input__icon el-icon-notebook-2"
                      style="cursor: pointer"
                    ></i>
                  </el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column width="150" label="预收项目类型">
              <template slot-scope="scope">
                <el-form-item
                  prop="kingFinanceReceiptDetailEntities.prepaidItemType"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-select
                    v-if="scope.row.payReceiptPurposeTitle === '预收款'"
                    v-model="scope.row.prepaidItemType"
                    placeholder="请选择预收项目类型"
                    style="width: 100%"
                  >
                    <el-option key="1" label="客户" value="2"></el-option>
                    <el-option key="2" label="销售订单" value="1"></el-option>
                  </el-select>
                  <el-input v-else disabled></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column width="150" label="预售销售订单">
              <template slot-scope="scope">
                <el-form-item
                  prop="kingFinanceReceiptDetailEntities.orderId"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input
                    :disabled="
                      scope.row.prepaidItemType == '2' ||
                      scope.row.payReceiptPurposeTitle !== '预收款'
                    "
                    v-model="scope.row.orderId"
                  >
                    <i
                      v-if="
                        scope.row.prepaidItemType === '1' &&
                        scope.row.payReceiptPurposeTitle === '预收款'
                      "
                      slot="suffix"
                      @click="addDialog('orderCode', scope.$index)"
                      class="el-input__icon el-icon-notebook-2"
                      style="cursor: pointer"
                    ></i
                  ></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column width="150" label="应收金额">
              <template slot-scope="scope">
                <el-form-item
                  prop="kingFinanceReceiptDetailEntities.copeMoney"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input
                    @change="changeMoney(scope.row, scope.$index)"
                    v-model="scope.row.copeMoney"
                  ></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column width="150" label="现金折扣">
              <template slot-scope="scope">
                <el-form-item
                  prop="kingFinanceReceiptDetailEntities.cashDiscount"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input
                    @change="changeMoney(scope.row, scope.$index)"
                    v-model="scope.row.cashDiscount"
                  ></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column width="150" label="手续费">
              <template slot-scope="scope">
                <el-form-item
                  prop="kingFinanceReceiptDetailEntities.handlingFees"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input
                    @change="changeMoney(scope.row, scope.$index)"
                    v-model="scope.row.handlingFees"
                  ></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column width="150" label="长短款">
              <template slot-scope="scope">
                <el-form-item
                  prop="kingFinanceReceiptDetailEntities.longShortPay"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input
                    @change="changeMoney(scope.row, scope.$index)"
                    v-model="scope.row.longShortPay"
                  ></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column width="150" label="物料编码">
              <template slot-scope="scope">
                <el-form-item
                  prop="kingFinanceReceiptDetailEntities.materialCode"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input disabled v-model="scope.row.materialCode">
                  </el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column width="150" label="物料名称">
              <template slot-scope="scope">
                <el-form-item
                  prop="kingFinanceReceiptDetailEntities.materialName"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input v-model="scope.row.materialName"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column width="150" label="我方银行账号">
              <template slot-scope="scope">
                <el-form-item
                  prop="kingFinanceReceiptDetailEntities.bankNumber"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input v-model="scope.row.bankNumber"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column width="150" label="结算号">
              <template slot-scope="scope">
                <el-form-item
                  prop="kingFinanceReceiptDetailEntities.settlementNumber"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input v-model="scope.row.settlementNumber"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column width="150" label="销售订单号">
              <template slot-scope="scope">
                <el-form-item
                  prop="kingFinanceReceiptDetailEntities.orderId"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input v-model="scope.row.orderId"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column width="150" label="含税单价">
              <template slot-scope="scope">
                <el-form-item
                  prop="kingFinanceReceiptDetailEntities.taxPrice"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input v-model="scope.row.taxPrice"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column width="150" label="费用项目">
              <template slot-scope="scope">
                <el-form-item
                  prop="kingFinanceReceiptDetailEntities.feeCode"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input v-model="scope.row.feeCode"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column width="150" label="费用承担部门">
              <template slot-scope="scope">
                <el-form-item
                  prop="kingFinanceReceiptDetailEntities.feeBearDeptCode"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input v-model="scope.row.feeBearDeptCode"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column width="150" label="实收金额">
              <template slot-scope="scope">
                <el-form-item
                  prop="kingFinanceReceiptDetailEntities.actualMoney"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input v-model="scope.row.actualMoney" disabled></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column width="150" label="折后金额">
              <template slot-scope="scope">
                <el-form-item
                  prop="kingFinanceReceiptDetailEntities.discountedMoney"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input
                    v-model="scope.row.discountedMoney"
                    disabled
                  ></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column width="150" label="预估税率">
              <template slot-scope="scope">
                <el-form-item
                  prop="kingFinanceReceiptDetailEntities.expectTaxRate"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input v-model="scope.row.expectTaxRate"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column width="150" label="税额">
              <template slot-scope="scope">
                <el-form-item
                  prop="kingFinanceReceiptDetailEntities.taxRate"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input v-model="scope.row.taxRate"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column width="150" label="应收金额不含税">
              <template slot-scope="scope">
                <el-form-item
                  prop="kingFinanceReceiptDetailEntities.noTaxAmount"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input v-model="scope.row.noTaxAmount"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
          </el-table>
        </el-collapse-item>
      </el-collapse>
    </el-form>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialog"
      append-to-body
      width="70%"
      destroy-on-close
      v-if="dialog"
    >
      <component
        :is="currentCom"
        :id="rowId"
        :visible.sync="dialog"
        :deptLevel="deptLevel"
        @getSelectVal="getSelectVal"
      ></component>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelSelect">取 消</el-button>
        <el-button type="primary" @click="confirmSelect">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getDocTypeList } from "@/api/system/purchase";
import { getCurrencyData } from "@/api/base/data";
import { addInfo, addInfoTemporary } from "@/api/finance/receipt";
import currency from "./dialog/currency.vue";
import payReceiptPurposeCode from "./dialog/payReceiptPurposeCode.vue";
import orderCode from "./dialog/orderCode.vue";
import recOrder from "./dialog/recOrder.vue";
import transactionCompany from "./dialog/transactionCompany.vue";
import sale from "./dialog/saleUser.vue";
import settlementType from "./dialog/settlementType.vue";
import customer from "./dialog/customer.vue";
import dept from "@/components/dialog/dept.vue";
import supplier from "@/components/dialog/supplier.vue";
import user from "@/components/dialog/user.vue";
import bank from "@/components/dialog/bank.vue";
export default {
  props: ["rowId"],
  components: {
    currency,
    payReceiptPurposeCode,
    orderCode,
    recOrder,
    transactionCompany,
    sale,
    settlementType,
    customer,
    dept,
    supplier,
    user,
    bank,
  },
  data() {
    return {
      activeName: ["basic", "detail"],
      dialog: false,
      counties: [],
      orgData: [], //创建组织弹窗列表
      orgSelect: [], //选中项
      orgTotal: "", //创建组织弹窗分页
      dialogType: "",
      dialogIndex: "",
      currentCom: "", //弹窗组件
      dialogTitle: "", //弹窗标题
      selectItem: [],
      gobackType: "",
      payType: "",
      deptLevel: "",
      currentType: "",
      props: {},
      formObject: {
        kingFinanceReceiptDetailEntities: [],
        kingFinanceReceiptEntity: {
          businessType: "16",
          modelId: this.$store.state.workflow.businessConfig.id,
          actualMoney: "",
          businessTime: "",
          createBy: "",
          createTime: "",
          deptId: "",
          docNo: "",
          docTypeCode: "",
          id: "",
          kingBaseCurrencyCode: "",
          currencyName: "",
          pageNo: 0,
          pageSize: 0,
          params: {},
          payDeptId: "",
          payDeptName: "",
          payDeptType: "",
          receiptBillId: "",
          settlementCurrencyCode: "", //结算币别
          settlementCurrencyName: "", //
          receiptAmountCurrency: "", //
          actualMoneyCurrency: "", //
          copeMoneyCurrency: "", //
          payAmount: "", //
          receiptDeptId: "",
          receiptDeptTitle: "",
          copeMoney: "",
          remark: "",
          saleDeptId: "",
          saleUserId: "",
          saleUserName: "",
          searchValue: "",
          sourceDocType: 0,
          transactionCompanyId: "",
          transactionCompanyType: "",
          transactionCompanyTitle: "",
          updateBy: "",
          updateTime: "",
        },
        kingInvoiceReceivableEntities: [],
      },
      rules: {
        kingSettlementTypeTitle: [
          {
            required: true,
            message: "请选择结算方式",
            trigger: "change",
          },
        ],
        payReceiptPurposeTitle: [
          {
            required: true,
            message: "请选择收款用途",
            trigger: "change",
          },
        ],
        kingFinanceReceiptEntity: {
          docTypeCode: [
            {
              required: true,
              message: "请选择单据类型",
              trigger: "change",
            },
          ],
          payDeptName: [
            {
              required: true,
              message: "请选择付款单位",
              trigger: "change",
            },
          ],
          payDeptType: [
            {
              required: true,
              message: "请选择付款单位类型",
              trigger: "change",
            },
          ],

          transactionCompanyType: [
            {
              required: true,
              message: "请选择往来单位类型",
              trigger: "change",
            },
          ],
          transactionCompanyTitle: [
            {
              required: true,
              message: "请选择往来单位",
              trigger: "change",
            },
          ],

          businessTime: [
            {
              required: true,
              message: "请选择业务日期",
              trigger: "change",
            },
          ],
          receiptDeptTitle: [
            {
              required: true,
              message: "请选择收款组织",
              trigger: "change",
            },
          ],
          currencyName: [
            {
              required: true,
              message: "请选择币别",
              trigger: "change",
            },
          ],
        },
      },

      documentTypeList: [],
    };
  },
  methods: {
    // 页签切换事件
    handleClick(tab, event) {},
    // 附件上传
    handleFileChange() {
      console.log("文件上传");
    },

    //提交
    submit() {
      const cb = (res) => {
        const obj = {
          id: res.data,
          businessType: "16",
          modelId: this.$store.state.workflow.businessConfig.id,
        };
        addInfo(obj).then((res) => {
          if (res.code === 200) {
            this.$store.commit("workflow/SET_BUSINESSDRAWER", false);
            this.$message({
              message: "提交成功！",
              type: "success",
            });
          }
        });
      };
      this.temporaryStorage(cb);
    },
    //暂存
    temporaryStorage(cb) {
      this.$refs["formRef"].validate((valid) => {
        if (!valid) {
          this.$message.error("请填写完整信息！");
          return;
        }
        // TODO 提交表单
        if (this.formObject.kingFinanceReceiptDetailEntities.length) {
          this.formObject.kingFinanceReceiptDetailEntities.forEach(
            (item, index) => {
              item.lineNumber = index + 1;
            }
          );
        }
        addInfoTemporary(this.formData).then((res) => {
          if (res.code === 200) {
            if (cb) {
              cb(res);
              return;
            }
            this.$store.commit("workflow/SET_BUSINESSDRAWER", false);
            this.$message({
              message: "暂存成功！",
              type: "success",
            });
          }
        });
      });
    },

    // 基础表单取消
    formRest(basicFormRef) {
      this.$refs[basicFormRef].resetFields();
    },
    // 删除行
    handleDataDelete(index, row, type) {
      this.$confirm("请确认是否要删除此行？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          if (type == "kingFinanceReceiptDetailEntities") {
            // 交货明细
            this.formObject.kingFinanceReceiptDetailEntities.splice(index, 1);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 物料明细 添加行
    addOrderDetails(e, type) {
      // 物料明细 obj
      let obj = {
        actualMoney: "",
        bankNumber: "",
        cashDiscount: "",
        createBy: "",
        createTime: "",
        discountedMoney: "",
        expectTaxRate: "",
        feeBearDeptCode: "",
        feeCode: "",
        handlingFees: "",
        id: "",
        copeMoney: "",
        kingSettlementTypeCode: "",
        kingSettlementTypeTitle: "",
        longShortPay: "",
        materialCode: "",
        money: "",
        noTaxAmount: "",
        num: "",
        orderCode: "",
        orderId: "",
        pageNo: 0,
        pageSize: 0,
        params: {},
        payReceiptPurposeCode: "",
        prepaidItemType: "",
        relationId: "",
        remark: "",
        remarks: "",
        searchValue: "",
        settlementNumber: "",
        sourceDocType: this.sourceDocType,
        sourceId: "",
        sourceNo: "",
        taxPrice: "",
        taxRate: "",
        transactionTime: "",
        type: 0,
        updateBy: "",
        updateTime: "",
      };
      if (e) {
        for (const key in obj) {
          if (e.hasOwnProperty(key) && key != "createTime") {
            if (key === "id") {
              if (type === "edit") {
                obj.materialId = e.id;
              } else {
                obj.sourceId = e.id;
                obj.sourceNo = e.docNo;
              }
            } else {
              obj[key] = e[key];
            }
          }
        }
        if (type === "edit") {
          this.$set(
            this.formObject.kingFinanceReceiptDetailEntities,
            this.dialogIndex,
            obj
          );
        } else {
          obj.money = e.totalTaxAmount;
          this.formObject.kingFinanceReceiptDetailEntities.push(obj);
        }
      } else {
        this.formObject.kingFinanceReceiptDetailEntities.push(obj);
      }
    },

    //添加新行
    addNewRow(type) {
      if (type === "kingFinanceReceiptDetailEntities") {
        this.addOrderDetails(); // 明细
      }
    },
    // 处理弹窗弹出
    handleOpen() {
      const nameMap = {
        currency: () => {
          this.dialogTitle = "选择币别";
          this.currentCom = currency;
        },
        settlementCurrencyName: () => {
          this.dialogTitle = "选择结算币别";
          this.currentCom = currency;
        },
        saleId: () => {
          this.dialogTitle = "选择销售员";
          this.currentCom = sale;
        },
        settlementType: () => {
          this.dialogTitle = "选择结算方式";
          this.currentCom = settlementType;
        },
        payReceiptPurposeCode: () => {
          this.dialogTitle = "选择收款用途";
          this.currentCom = payReceiptPurposeCode;
        },
        orderCode: () => {
          this.dialogTitle = "选择销售订单";
          this.currentCom = orderCode;
        },
        recOrder: () => {
          this.dialogTitle = "选择应收单";
          this.currentCom = recOrder;
        },
        otherOrder: () => {
          this.dialogTitle = "选择其他应付单";
          this.currentCom = otherOrder;
        },
        payDeptName: () => {
          this.dialogTitle = "选择付款单位";
          this.currentCom = transactionCompany;
        },
        receiptDeptTitle: () => {
          this.dialogTitle = "选择收款组织";
          this.currentCom = transactionCompany;
        },
        Supplier: () => {
          this.dialogTitle = "选择付款单位";
          this.currentCom = supplier;
        },
        Department: () => {
          this.dialogTitle = "选择付款单位";
          this.currentCom = dept;
          this.deptLevel = 1;
        },
        Empinfo: () => {
          this.dialogTitle = "选择付款单位";
          this.currentCom = user;
        },
        OTHERS: () => {
          this.dialogTitle = "选择付款单位";
          this.currentCom = transactionCompany;
        },
        Organization: () => {
          this.dialogTitle = "选择付款单位";
          this.currentCom = dept;
          this.deptLevel = 2;
        },
        BANK: () => {
          this.dialogTitle = "选择付款单位";
          this.currentCom = bank;
        },
        Customer: () => {
          this.dialogTitle = "选择付款单位";
          this.currentCom = customer;
        },
      };
      nameMap[this.dialogType] ? nameMap[this.dialogType]() : "";
    },
    // 选择弹窗 item
    getSelectVal(val) {
      this.selectItem = val;
    },
    //弹出dialog
    addDialog(type, index) {
      this.currentType = type;
      let t = type;
      if (type === "transactionCompany") {
        if (!this.gobackType) {
          return;
        }
        if (this.gobackType === "BD_Supplier") {
          t = "Supplier";
        } else if (this.gobackType === "BD_Department") {
          t = "Department";
        } else if (this.gobackType === "BD_Empinfo") {
          t = "Empinfo";
        } else if (this.gobackType === "FIN_OTHERS") {
          t = "OTHERS";
        } else if (this.gobackType === "ORG_Organization") {
          t = "Organization";
        } else if (this.gobackType === "BD_BANK") {
          t = "BANK";
        } else if (this.gobackType === "BD_Customer") {
          t = "Customer";
        }
      }
      if (type === "payDeptName") {
        if (!this.payType) {
          return;
        }
        if (this.payType === "BD_Supplier") {
          t = "Supplier";
        } else if (this.payType === "BD_Department") {
          t = "Department";
        } else if (this.payType === "BD_Empinfo") {
          t = "Empinfo";
        } else if (this.payType === "FIN_OTHERS") {
          t = "OTHERS";
        } else if (this.payType === "ORG_Organization") {
          t = "Organization";
        } else if (this.payType === "BD_BANK") {
          t = "BANK";
        } else if (this.payType === "BD_Customer") {
          t = "Customer";
        }
      }
      this.dialog = true;
      this.dialogType = t;
      this.dialogIndex = index;
      this.handleOpen();
    },
    //获取单据类型
    getDocTypeListApi() {
      getDocTypeList({ docCode: "AR_RECEIVEBILL" }).then((res) => {
        if (res.code === 200) {
          this.documentTypeList = res.rows;
        }
      });
    },

    //基础实付和应付
    changeReclMoney() {
      //基础应收
      const copeMoney = this.formObject.kingFinanceReceiptDetailEntities.reduce(
        (total, item) => {
          return total + item.copeMoney * 1;
        },
        0
      );
      this.formObject.kingFinanceReceiptEntity.copeMoney = copeMoney;
      this.formObject.kingFinanceReceiptEntity.copeMoneyCurrency = copeMoney;
      //基础实收
      const actualMoney =
        this.formObject.kingFinanceReceiptDetailEntities.reduce(
          (total, item) => {
            return (
              total +
              item.actualMoney *
                this.formObject.kingFinanceReceiptEntity.settlementRate *
                1
            );
          },
          0
        );
      this.formObject.kingFinanceReceiptEntity.actualMoney = actualMoney;
      this.formObject.kingFinanceReceiptEntity.actualMoneyCurrency =
        actualMoney;
    },

    //现金折扣
    changeMoney(row, index) {
      row.discountedMoney = row.copeMoney * 1 - row.cashDiscount * 1;
      row.actualMoney =
        row.discountedMoney * 1 - row.longShortPay * 1 + row.handlingFees * 1;
      this.changeReclMoney();
      //行内
      this.$set(this.formObject.kingFinanceReceiptDetailEntities, index, row);
    },
    //下拉类型选择
    handleCommand(val) {
      this.dialogType = val;
      this.dialog = true;
      if (val === "recOrder") {
        this.sourceDocType = 3;
      } else if (val === "otherOrder") {
        this.sourceDocType = 4;
      } else if (val === "engineering") {
        this.sourceDocType = 5;
      }
      this.handleOpen();
    },
    //单位类型
    gobackTypeChange(val) {
      this.gobackType = val;
      this.formObject.kingFinanceReceiptEntity.transactionCompanyId = "";
      this.formObject.kingFinanceReceiptEntity.transactionCompanyTitle = "";
    },
    payTypeChange(val) {
      console.log("payTypeChange", val);
      this.payType = val;
      this.formObject.kingFinanceReceiptEntity.payDeptId = "";
      this.formObject.kingFinanceReceiptEntity.payDeptName = "";
    },
    cancelSelect() {
      this.dialog = false;
      this.selectItem = [];
    },
    confirmSelect() {
      const nameMap = {
        currency: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          this.formObject.kingFinanceReceiptEntity.kingBaseCurrencyCode =
            this.selectItem[0].code;
          this.formObject.kingFinanceReceiptEntity.currencyName =
            this.selectItem[0].currencyName;
          this.formObject.kingFinanceReceiptEntity.settlementCurrencyCode =
            this.selectItem[0].code;
          this.formObject.kingFinanceReceiptEntity.settlementCurrencyName =
            this.selectItem[0].currencyName;
          this.formObject.kingFinanceReceiptEntity.rate =
            this.selectItem[0].rate;
          this.formObject.kingFinanceReceiptEntity.settlementRate =
            this.selectItem[0].rate;
          this.dialog = false;
        },
        settlementCurrencyName: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          this.formObject.kingFinanceReceiptEntity.settlementCurrencyCode =
            this.selectItem[0].code;
          this.formObject.kingFinanceReceiptEntity.settlementCurrencyName =
            this.selectItem[0].currencyName;
          this.formObject.kingFinanceReceiptEntity.settlementRate =
            this.selectItem[0].rate;
        },
        transactionCompany: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          this.formObject.kingFinanceReceiptEntity.transactionCompanyId =
            this.selectItem[0].id;
          this.formObject.kingFinanceReceiptEntity.transactionCompanyTitle =
            this.selectItem[0].title;
          this.dialog = false;
        },
        saleId: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          this.formObject.kingFinanceReceiptEntity.saleUserId =
            this.selectItem[0].userId;
          this.formObject.kingFinanceReceiptEntity.saleUserName =
            this.selectItem[0].userName;

          this.dialog = false;
        },
        settlementType: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          this.formObject.kingFinanceReceiptDetailEntities[
            this.dialogIndex
          ].kingSettlementTypeCode = this.selectItem[0].bizCode;
          this.formObject.kingFinanceReceiptDetailEntities[
            this.dialogIndex
          ].kingSettlementTypeTitle = this.selectItem[0].title;
          this.dialog = false;
        },
        payReceiptPurposeCode: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          const row = {
            ...this.formObject.kingFinanceReceiptDetailEntities[
              this.dialogIndex
            ],
            payReceiptPurposeCode: this.selectItem[0].bizCode,
            payReceiptPurposeTitle: this.selectItem[0].title,
          };
          this.$set(
            this.formObject.kingFinanceReceiptDetailEntities,
            this.dialogIndex,
            row
          );
          this.dialog = false;
        },
        recOrder: () => {
          if (this.selectItem.length < 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          this.selectItem.forEach((e) => {
            this.formObject.kingInvoiceReceivableEntities.push(e);
            this.addOrderDetails(e);
          });
          this.dialog = false;
        },
        orderCode: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          this.formObject.kingFinanceReceiptDetailEntities[
            this.dialogIndex
          ].orderId = this.selectItem[0].saleOrderId;
          this.formObject.kingFinanceReceiptDetailEntities[
            this.dialogIndex
          ].money = this.selectItem[0].unpaidAmount;
          this.changeReclMoney();
          this.dialog = false;
        },
        payDeptName: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          this.formObject.kingFinanceReceiptEntity.payDeptId =
            this.selectItem[0].id;
          this.formObject.kingFinanceReceiptEntity.payDeptName =
            this.selectItem[0].title;
          this.dialog = false;
        },
        receiptDeptTitle: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          this.formObject.kingFinanceReceiptEntity.receiptDeptId =
            this.selectItem[0].id;
          this.formObject.kingFinanceReceiptEntity.receiptDeptTitle =
            this.selectItem[0].title;
          this.dialog = false;
        },
        Supplier: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          if (this.currentType === "transactionCompany") {
            this.formObject.kingFinanceReceiptEntity.transactionCompanyId =
              this.selectItem[0].id;
            this.formObject.kingFinanceReceiptEntity.transactionCompanyTitle =
              this.selectItem[0].supplierName;
          } else if (this.currentType === "payDeptName") {
            this.formObject.kingFinanceReceiptEntity.payDeptId =
              this.selectItem[0].id;
            this.formObject.kingFinanceReceiptEntity.payDeptName =
              this.selectItem[0].supplierName;
            this.dialog = false;
          }
          this.dialog = false;
        },
        Department: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          if (this.currentType === "transactionCompany") {
            this.formObject.kingFinanceReceiptEntity.transactionCompanyId =
              this.selectItem[0].deptId;
            this.formObject.kingFinanceReceiptEntity.transactionCompanyTitle =
              this.selectItem[0].deptName;
          } else if (this.currentType === "payDeptName") {
            this.formObject.kingFinanceReceiptEntity.payDeptId =
              this.selectItem[0].deptId;
            this.formObject.kingFinanceReceiptEntity.payDeptName =
              this.selectItem[0].deptName;
            this.dialog = false;
          }
          this.dialog = false;
        },
        Empinfo: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          if (this.currentType === "transactionCompany") {
            this.formObject.kingFinanceReceiptEntity.transactionCompanyId =
              this.selectItem[0].deptId;
            this.formObject.kingFinanceReceiptEntity.transactionCompanyTitle =
              this.selectItem[0].userName;
          } else if (this.currentType === "payDeptName") {
            this.formObject.kingFinanceReceiptEntity.payDeptId =
              this.selectItem[0].deptId;
            this.formObject.kingFinanceReceiptEntity.payDeptName =
              this.selectItem[0].userName;
            this.dialog = false;
          }
          this.dialog = false;
        },
        OTHERS: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          if (this.currentType === "transactionCompany") {
            this.formObject.kingFinanceReceiptEntity.transactionCompanyId =
              this.selectItem[0].id;
            this.formObject.kingFinanceReceiptEntity.transactionCompanyTitle =
              this.selectItem[0].title;
          } else if (this.currentType === "payDeptName") {
            this.formObject.kingFinanceReceiptEntity.payDeptId =
              this.selectItem[0].id;
            this.formObject.kingFinanceReceiptEntity.payDeptName =
              this.selectItem[0].title;
            this.dialog = false;
          }
          this.dialog = false;
        },
        Organization: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          if (this.currentType === "transactionCompany") {
            this.formObject.kingFinanceReceiptEntity.transactionCompanyId =
              this.selectItem[0].deptId;
            this.formObject.kingFinanceReceiptEntity.transactionCompanyTitle =
              this.selectItem[0].deptName;
          } else if (this.currentType === "payDeptName") {
            this.formObject.kingFinanceReceiptEntity.payDeptId =
              this.selectItem[0].deptId;
            this.formObject.kingFinanceReceiptEntity.payDeptName =
              this.selectItem[0].deptName;
            this.dialog = false;
          }
          this.dialog = false;
        },
        BANK: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          if (this.currentType === "transactionCompany") {
            this.formObject.kingFinanceReceiptEntity.transactionCompanyId =
              this.selectItem[0].id;
            this.formObject.kingFinanceReceiptEntity.transactionCompanyTitle =
              this.selectItem[0].title;
          } else if (this.currentType === "payDeptName") {
            this.formObject.kingFinanceReceiptEntity.payDeptId =
              this.selectItem[0].id;
            this.formObject.kingFinanceReceiptEntity.payDeptName =
              this.selectItem[0].title;
            this.dialog = false;
          }
          this.dialog = false;
        },
        Customer: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          if (this.currentType === "transactionCompany") {
            this.formObject.kingFinanceReceiptEntity.transactionCompanyId =
              this.selectItem[0].id;
            this.formObject.kingFinanceReceiptEntity.transactionCompanyTitle =
              this.selectItem[0].customerName;
          } else if (this.currentType === "payDeptName") {
            this.formObject.kingFinanceReceiptEntity.payDeptId =
              this.selectItem[0].id;
            this.formObject.kingFinanceReceiptEntity.payDeptName =
              this.selectItem[0].customerName;
            this.dialog = false;
          }
          this.dialog = false;
        },
      };
      nameMap[this.dialogType] ? nameMap[this.dialogType]() : "";
    },
  },
  created() {
    //单据类型
    this.getDocTypeListApi();
    //币别
    getCurrencyData({
      page: 1,
      pageSize: 1000,
    }).then((res) => {
      if (res.rows.length) {
        this.formObject.kingFinanceReceiptEntity.kingBaseCurrencyCode =
          res.rows[0].code;
        this.formObject.kingFinanceReceiptEntity.currencyName =
          res.rows[0].currencyName;
        this.formObject.kingFinanceReceiptEntity.settlementCurrencyCode =
          res.rows[0].code;
        this.formObject.kingFinanceReceiptEntity.settlementCurrencyName =
          res.rows[0].currencyName;
        this.formObject.kingFinanceReceiptEntity.settlementRate =
          res.rows[0].rate;
        this.formObject.kingFinanceReceiptEntity.rate = res.rows[0].rate;
      }
    });
  },
};
</script>
<style lang="scss" scoped>
// .add {
//   padding: 15px;
// }
.panel-title {
  font-size: 18px;
  color: #37474f;
  font-weight: 500;
  text-shadow: rgba(0, 0, 0, 0.15) 0 0 1px;
}

::v-deep .el-form-item__content {
  margin-left: 0;
}

.main-responsible {
  font-size: 14px;
  font-weight: 550;
  color: #263238;
  position: relative;
  text-indent: 15px;
}
::v-deep .el-table td.el-table__cell div {
  overflow: visible;
}

::v-deep .el-table td.el-table__cell {
  padding-bottom: 20px;
}
</style>
